define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Grabseckill, $window, $timeout, $ionicSlideBoxDelegate, $ionicSideMenuDelegate, GoPage, $stateParams,GoDialog,GoPicture) {
		$scope.input = new Object();
		$scope.input.id = $stateParams.id;
		$scope.input.name = $stateParams.name;
		$scope.input.start_time = $stateParams.start_time;
		$scope.input.end_time = $stateParams.end_time;
		var region = new Array();
		var member = new Array();
		$scope.store = new Array();
		$scope.input.region_select = { region_id: 0, region_name: "请选择区域" };
		$scope.friends = new Array();
		$scope.member_change = true;
		$scope.show_region = new Array();
		var show_store = new Array();
//图片移除
		$scope.ondelete = function(index) {
			$scope.detail.image.splice(index, 1);
		};
		//动态响应
		$scope.doShowFile = function() {
			document.getElementById("upfilebutton").click();
		}
		//图片上传
		$scope.onupload = function(val) {
			var fileList = val.files;
			for(var i = 0; i < val.files.length; i++) {
				var reader = new FileReader();
				reader.readAsDataURL(fileList[i]);
				var file = fileList[i];
				reader.onload = function(e) {
					//图片进行压缩处理
					GoPicture.compress(file,e.target.result, 50, function(base64) {
						$scope.$apply(function() {
							var i=$scope.detail.image.length;
							$scope.detail.image[i] = {
								"id":'',
								"image":base64
							}
							val.value = "";
						});
					});
				}
			}
		    //上传成功
			val.value = "";
		};
		//张--全部终端用户、全部好友
		$scope.isAlluser = true;
		$scope.isAllfriend = false;
		$scope.chang_user = function() {
			$scope.isAlluser = true;
			$scope.isAllfriend = false;
			$scope.choose_all($scope.input.is_all);

		}
		$scope.chang_friend = function() {
			$scope.isAllfriend = true;
			$scope.isAlluser = false;
			//			console.log($scope.store_change);
			if($scope.member_change == true)
				$scope.get_friends();
		}
		$scope.get_friends = function() {
			if($scope.store.length > 0) {
				$scope.get_member();
				console.log($scope.members);
				console.log(member.length);
				if(member.length != 0)
					Grabseckill.friends_list($scope.input.region, member, function(data) {
						$scope.friends = data.member;
						$scope.member_change = false;
						$scope.input.friend_all == false;


					});
							else
							$scope.friends = new Array();

			}
		}
		$scope.isLoad = false; //可以加载
		//画面加载
		$scope.$on('$ionicView.afterEnter', function() {
			//动态设置高度
			var all = $window.innerHeight;
			//计算滚动条
			$scope.myObj = {
				"height": all - 44 + "px"
			};
			$scope.init_data();
		}, false);
		//滑动时候触发
		$scope.selectIndex = 0;
		$scope.slideChanged = function(index) {
			//手指滑动
			$scope.selectIndex = index;
		};
		$scope.activeSlide = function(index) {
			$scope.selectIndex = index;
			$ionicSlideBoxDelegate.$getByHandle('main-handle').slide(index);
			//判断关闭
			if($ionicSideMenuDelegate.isOpenRight()) {
				$ionicSideMenuDelegate.toggleRight();
			}
		};
		//下拉刷新 判断哪个模块进行刷新
		$scope.doRefresh = function() {
			$timeout(function() {
				$scope.$broadcast('scroll.refreshComplete');
			}, 1000);
		};
		//加载更多
		$scope.doRefreshMore = function() {
			$timeout(function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
			}, 1000);
		};
		$scope.doSelectRegion = function () {
			var list = new Array();
			$scope.members.region.forEach(obj => {
				if (obj.id == undefined)
					obj.id = obj.region_id;
				if (obj.name == undefined)
					obj.name = obj.region_name;
			});
			//id是值 name是显示
			GoDialog.showSelect("区域", $scope.members.region, $scope.input.region_select, function (data) {
				$scope.input.region = data.region_id;
				$scope.input.region_select = data;
			});
		}

		$scope.choose_all = function() {
			if($scope.input.member_all == true) {
				if($scope.store.length > 0)
					for(var i = 0; i < $scope.store.length; i++) {
						$scope.store[i].checked = true;
					}
			}

			if($scope.input.friend_all == true) {
				if($scope.friends != undefined)
					if($scope.friends.length > 0)
						for(var i = 0; i < $scope.friends.length; i++) {
							$scope.friends[i].checked = true;

						}
			}

		}
		$scope.tap_member_all = function() {
			if($scope.input.member_all == true) {
				if($scope.store.length > 0)
					for(var i = 0; i < $scope.store.length; i++) {
						$scope.store[i].checked = true;
					}
			} else {
				if($scope.store.length > 0)
					for(var i = 0; i < $scope.store.length; i++) {
						$scope.store[i].checked = false;
					}
			}

			
		}
		$scope.tap_friend_all = function(){
			if($scope.input.friend_all == true) {
				if($scope.friends != undefined)
					if($scope.friends.length > 0)
						for(var i = 0; i < $scope.friends.length; i++) {
							$scope.friends[i].checked = true;

						}
			} else {
				if($scope.friends != undefined)
					if($scope.friends.length > 0)
						for(var i = 0; i < $scope.friends.length; i++) {
							$scope.friends[i].checked = false;

						}
			}
		}
		$scope.member_tap = function(id) {
			console.log(id);
			$scope.member_change = true;
			for(var i = 0; i < $scope.store.length; i++) {
				if($scope.store[i].member_id == id) {
					console.log(id);
					if($scope.store[i].checked == false) {
						$scope.input.member_all = false;
						break;
					} else {
						if($scope.store.length == 1)
							$scope.input.member_all = true;
					}
				} else if($scope.store[i].checked == true) {
					$scope.input.member_all = true;

				} else {
					$scope.input.member_all = false;
					break;
				}
			}
			console.log($scope.members);
		}
		$scope.friend_tap = function(id) {

			for(var i = 0; i < $scope.friends.length; i++) {
				if($scope.friends[i].member_id == id) {
					if($scope.friends[i].checked == false) {
						$scope.input.friend_all = false;
						break;
					} else {
						if($scope.friends.length == 1)
							$scope.input.friend_all = true;
					}
				} else if($scope.friends[i].checked == true) {
					$scope.input.friend_all = true;

				} else {
					$scope.input.friend_all = false;
					break;
				}
			}
		}
		$scope.get_member = function() {
			member = new Array();
			show_store = new Array();
			console.log($scope.store);
			console.log($scope.members);
			for(var i = 0; i < $scope.store.length; i++) {
				if($scope.store[i].checked == true) {
					var n = member.length;
					member[n] = {
						"member_id": $scope.store[i].member_id
					};
					show_store[n] ={
						"store_name":$scope.store[i].member_name
					}
				}
			}
				for(var i = 0; i < $scope.friends.length; i++) {
					if($scope.friends[i].checked == true) {
						var n = member.length;
						member[n] = {
							"member_id": $scope.friends[i].member_id
						};
						show_store[n] = {
							"store_name":$scope.friends[i].name
						}
					}
				}
			console.log(member);
			console.log(show_store);
		}
		$scope.get_region = function() {
			$scope.get_member();
			for(var i = 0; i < region.length; i++) {
				if(region[i].region_id == $scope.input.region) {
					console.log(1);
					region[i] = {
						"region_id": $scope.input.region,
						"is_all": $scope.input.member_all,
						"member": member
					};
					$scope.show_region[i] = {
						"region_name":$scope.input.region,
						"store":show_store
					};
					return;
				}
			}
			for(var i=0;i<$scope.members.region.length;i++){
				if($scope.input.region==$scope.members.region[i].region_id)
				$scope.input.region_name = $scope.members.region[i].region_name;
			}
			var n = region.length;
			region[n] = {
				"region_id": $scope.input.region,
				"is_all": $scope.input.member_all,
				"member": member
			};
			$scope.show_region[n] = {
				"region_name":$scope.input.region_name,
				"store":show_store
			};
		}
		$scope.delete_store = function(region_name,index){
			for(var i=0;i<$scope.show_region.length;i++){
				if($scope.show_region[i].region_name==region_name){
					region[i].member.splice(index,1);
			       $scope.show_region[i].store.splice(index,1);
				}
			}
			
		}
		$scope.delete_region = function(index){
			$scope.show_region.splice(index,1);
			region.splice(index,1);
		}
		$scope.submit = function() {
//			$scope.get_region();
			console.log($scope.members);
			console.log(region);
			if(region.length<1){
				GoDialog.toast("请选择代理商");
				return;
			}
			for(var i =0;i<region.length;i++){
				if(region[i].member==undefined){
					GoDialog.toast("请填写完整的代理商信息");
				return;
				}else if(region[i].member.length<1){
						GoDialog.toast("请填写完整的代理商信息");
				return;
				}
			}
						Grabseckill.dealer_jion($scope.input.id,region,function(data){
							if(data.success==true){
								GoDialog.toast("保存成功");
							}else
							GoDialog.toast("保存失败");
						});
		}
		$scope.init_data = function() {

			Grabseckill.panicbuy_member($scope.input.id, function(data) {
				$scope.members = data;
				$scope.$watch('input.region', function(newValue, oldValue) {
					for(var i = 0; i < $scope.members.region.length; i++) {
						if($scope.members.region[i].region_id == newValue) {
							$scope.store = $scope.members.region[i].member;
							$scope.member_change = true;
							$scope.input.member_all = $scope.members.region[i].is_all;
							$scope.choose_all();
							$scope.get_friends();

						}
					}
				})
			});

			Grabseckill.panicbuy_detail($scope.input.id, function(data) {
				$scope.detail = data;

			});
			Grabseckill.goods_list($scope.input.id, function(data) {
				$scope.goods = data;
			})
		}

		$scope.init_data();
	}

	controller.$inject = ['$scope', 'Grabseckill', '$window', '$timeout', '$ionicSlideBoxDelegate', '$ionicSideMenuDelegate', 'GoPage', '$stateParams','GoDialog','GoPicture'];

	return app.registerController("grabseckillctrl",controller);
});